package com.wcl.demo.utils;

import android.content.Context;

import com.easemob.EMValueCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.util.EMLog;
import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.wcl.demo.model.entity.SearchHimUser;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/12/5.
 */
public class ParseManager {

    private static final String TAG = ParseManager.class.getSimpleName();
    private static final String ParseAppID = "UUL8TxlHwKj7ZXEUr2brF3ydOxirCXdIj9LscvJs";
    private static final String ParseClientKey = "B1jH9bmxuYyTcpoFfpeVslhmLYsytWTxqYqKQhBJ";

    private static final String CONFIG_TABLE_NAME = "searchhimuser";
    private static final String CONFIG_USERNAME = "username";
    private static final String CONFIG_NICK = "nickname";
    private static final String CONFIG_AVATAR = "avatar";

    private Context appContext;
    private static ParseManager instance = new ParseManager();


    private ParseManager() {
    }

    public static ParseManager getInstance() {
        return instance;
    }

    public void onInit(Context context) {
        this.appContext = context.getApplicationContext();
        Parse.enableLocalDatastore(appContext);
        Parse.initialize(context, ParseAppID, ParseClientKey);
    }

    /***
     * 修改昵称
     *
     * @param nickname
     * @return
     */
    public boolean updateParseNickName(final String nickname) {
        String username = EMChatManager.getInstance().getCurrentUser();
        ParseQuery<ParseObject> pQuery = ParseQuery.getQuery(CONFIG_TABLE_NAME);
        pQuery.whereEqualTo(CONFIG_USERNAME, username);
        ParseObject pUser = null;
        try {
            pUser = pQuery.getFirst();
            if (pUser == null) {
                return false;
            }
            pUser.put("nickname", nickname);
            pUser.save();
            return true;
        } catch (ParseException e) {
            if (e.getCode() == ParseException.OBJECT_NOT_FOUND) {
                pUser = new ParseObject("searchhimuser");
                pUser.put("username" , username);
                pUser.put("nickname" , nickname);
                try {
                    pUser.save();
                    return true;
                } catch (ParseException e1) {
                    e1.printStackTrace();
                }
            }

            e.printStackTrace();
        }
        return false;
    }


    public String uploadParseAvatar(byte[] data) {
        String username = EMChatManager.getInstance().getCurrentUser();
        ParseQuery<ParseObject> pQuery = ParseQuery.getQuery(CONFIG_TABLE_NAME);
        pQuery.whereEqualTo(CONFIG_USERNAME, username);
        ParseObject pUser = null;
        try {
            pUser = pQuery.getFirst();
            if (pUser == null) {
                pUser = new ParseObject(CONFIG_TABLE_NAME);
                pUser.put(CONFIG_USERNAME, username);
            }
            ParseFile pFile = new ParseFile(data);
            pUser.put(CONFIG_AVATAR, pFile);
            pUser.save();
            return pFile.getUrl();
        } catch (ParseException e) {
            if (e.getCode() == ParseException.OBJECT_NOT_FOUND) {
                try {
                    pUser = new ParseObject(CONFIG_TABLE_NAME);
                    pUser.put(CONFIG_USERNAME, username);
                    ParseFile pFile = new ParseFile(data);
                    pUser.put(CONFIG_AVATAR, pFile);
                    pUser.save();
                    return pFile.getUrl();
                } catch (ParseException e1) {
                    e1.printStackTrace();
                    EMLog.e(TAG, "parse error " + e1.getMessage());
                }
            } else {
                e.printStackTrace();
                EMLog.e(TAG, "parse error " + e.getMessage());
            }
        }
        return null;
    }


    public void getContactInfos(List<String> usernames, final EMValueCallBack<List<SearchHimUser>> callback) {
        ParseQuery<ParseObject> pQuery = ParseQuery.getQuery(CONFIG_TABLE_NAME);
        pQuery.whereContainedIn(CONFIG_USERNAME, usernames);
        pQuery.findInBackground(new FindCallback<ParseObject>() {

            @Override
            public void done(List<ParseObject> arg0, ParseException arg1) {
                if (arg0 != null) {
                    List<SearchHimUser> mList = new ArrayList<SearchHimUser>();
                    for (ParseObject pObject : arg0) {
                        SearchHimUser user = new SearchHimUser(pObject.getString(CONFIG_USERNAME));
                        ParseFile parseFile = pObject.getParseFile(CONFIG_AVATAR);
                        if (parseFile != null) {
                            user.setAvatar(parseFile.getUrl());
                        }
                        user.setNick(pObject.getString(CONFIG_NICK));
                        mList.add(user);
                    }
                    callback.onSuccess(mList);
                } else {
                    callback.onError(arg1.getCode(), arg1.getMessage());
                }
            }
        });
    }

}
